package com.zlt.utils;

import com.zlt.annotation.*;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.Date;
import java.util.*;

public class SqlUtils {

	private static DataSource dataSource = DataSourceUtil.getDataSource();

	/**
	 * 获取数据库连接
	 * @return
	 */
	public static Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 关闭资源
	 * @param rs
	 * @param pst
	 * @param conn
	 */
	public static void close(ResultSet rs,PreparedStatement pst,Connection conn) {
		if(rs != null)
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		if(pst != null)
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		if(conn != null)
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	/**
	 * 执行增删改
	 * @param sql
	 * @param args
	 * @return -1 连接异常
	 */
	public static int update(String sql,Object... args) {
		Connection connection = getConnection();
		if(connection != null) {
			PreparedStatement pst = null;
			int result = 0;//返回的结果
			try {
				//获取预处理器
				pst = connection.prepareStatement(sql);
				//赋值参数
				if(args != null) {
					for (int i = 0; i < args.length; i++) {
						pst.setObject((i + 1), args[i]);
					}
				}
				result = pst.executeUpdate();
			} catch (SQLException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
			} finally {
				close(null, pst, connection);
			}
			return result;
		}
		return -1;
	}
	
	
	/**
	 * 执行插入操作，返回id
	 * @param sql
	 * @param args
	 * @return -1 连接异常
	 */
	public static int insert(String sql,Object... args) {
		Connection connection = getConnection();
		if(connection != null) {
			PreparedStatement pst = null;
			ResultSet rs = null;
			int id = -1;//返回的结果
			try {
				//获取预处理器
				pst = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				//赋值参数
				if(args != null) {
					for (int i = 0; i < args.length; i++) {
						pst.setObject((i + 1), args[i]);
					}
				}
				pst.executeUpdate();
				//主键生成结果集
				rs = pst.getGeneratedKeys();
				if(rs.next()) {
					//获取生成的id
					id = rs.getInt(1);
				}
			} catch (SQLException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
			} finally {
				close(null, pst, connection);
			}
			return id;
		}
		return -1;
	}
	
	/**
	 * 修改对象
	 * update 表名  set 属性名=属性值,... where 主键=主键值
	 * @param obj
	 * @return
	 */
	public static int update(Object obj) {
		//获取到对象的类
		Class<? extends Object> clazz = obj.getClass();
		//获取表名
		if(clazz.isAnnotationPresent(Table.class)) {
			StringBuilder sqlBuilder = new StringBuilder();
			//存放id的sql部分
			StringBuilder idSqlBuilder = new StringBuilder();
			Object idValue = null;//id的值
			List params = new ArrayList<>();
			sqlBuilder.append("update ");
			idSqlBuilder.append(" where ");
			//获取注解对象
			Table table = clazz.getAnnotation(Table.class);
			String tableName = table.name();
			//如果注解的value为空串则表名为类名
			if("".equals(tableName)) {
				tableName = clazz.getSimpleName();
			}
			//拼接表名
			sqlBuilder.append(tableName);
			sqlBuilder.append(" set ");
			//处理属性
			Field[] fields = clazz.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				
				//如果对象的属性为null则不进行操作
				if(getFieldValue(obj, field) == null) {
					continue;
				}
				
				if(field.isAnnotationPresent(Column.class)) {
					field.setAccessible(true);//修改权限可以访问
					Column column = field.getAnnotation(Column.class);
					String fieldName = column.name();
					//如果注解的value为空串则表名为类名
					if("".equals(fieldName)) {
						fieldName = field.getName();
					}
					sqlBuilder.append(fieldName);
					sqlBuilder.append("=?,");
					//还要处理值 
					Object value = getFieldValue(obj, field);
					params.add(value);
				} else if(field.isAnnotationPresent(Id.class)) {
					field.setAccessible(true);//修改权限可以访问
					Id id = field.getAnnotation(Id.class);
					String fieldName = id.name();
					//如果注解的value为空串则表名为类名
					if("".equals(fieldName)) {
						fieldName = field.getName();
					}
					idSqlBuilder.append(fieldName);
					idSqlBuilder.append("=?");
					//还要处理值 
					idValue = getFieldValue(obj, field);
				}
			}
			
			//sql基本ok了
			String sql = sqlBuilder.substring(0, sqlBuilder.length()-1) + idSqlBuilder.toString();
			//参数ok
			params.add(idValue);
			//执行sql
			return update(sql, params.toArray());
			
		}
		return -1;
	}
	
	/**
	 * 删除数据
	 * delete from 表名 where 主键=主键值
	 * @param obj
	 * @return
	 */
	public static int delete(Object obj) {
		Class<? extends Object> clazz = obj.getClass();
		if(clazz.isAnnotationPresent(Table.class)) {
			StringBuilder sqlBuilder = new StringBuilder();
			//存放id的sql部分
			Object idValue = null;//id的值
			sqlBuilder.append("delete from ");
			//获取注解对象
			Table table = clazz.getAnnotation(Table.class);
			String tableName = table.name();
			//如果注解的value为空串则表名为类名
			if("".equals(tableName)) {
				tableName = clazz.getSimpleName();
			}
			//拼接表名
			sqlBuilder.append(tableName);
			sqlBuilder.append(" where ");
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				if(field.isAnnotationPresent(Id.class)) {
					field.setAccessible(true);//修改权限可以访问
					Id id = field.getAnnotation(Id.class);
					String fieldName = id.name();
					//如果注解的value为空串则表名为类名
					if("".equals(fieldName)) {
						fieldName = field.getName();
					}
					sqlBuilder.append(fieldName);
					sqlBuilder.append("=?");
					//还要处理值 
					idValue = getFieldValue(obj, field);
					break;
				}
			}
			
			return update(sqlBuilder.toString(), idValue);
		}
		return -1;
	}
	/**
	 * 插入数据
	 * insert into 表名 (字段名,字段名...)  values (?,?,....)
	 * @param obj
	 * @return
	 */
	public static int insert(Object obj) {
		Class<? extends Object> clazz = obj.getClass();
		if(clazz.isAnnotationPresent(Table.class)) {
			StringBuilder sqlBuilder = new StringBuilder();//sql语句拼接
			StringBuilder valuesSqlBuilder = new StringBuilder();//sql语句拼接
			//存放onetomany的属性
			List<List> oneToManyField = new ArrayList<List>();
			
			List params = new ArrayList();//参数
			sqlBuilder.append("insert into ");
			valuesSqlBuilder.append(" ) values ( ");
			//获取注解对象
			Table table = clazz.getAnnotation(Table.class);
			String tableName = table.name();
			//如果注解的value为空串则表名为类名
			if("".equals(tableName)) {
				tableName = clazz.getSimpleName();
			}
			//拼接表名
			sqlBuilder.append(tableName);
			sqlBuilder.append(" ( ");
			//开始拼接属性
			Field[] fields = clazz.getDeclaredFields();
			//循环属性
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				//如果对象的属性为null则不进行操作
				if(getFieldValue(obj, field) == null) {
					continue;
				}
				//如果是主键
				if(field.isAnnotationPresent(Id.class)) {
					field.setAccessible(true);
					
					Id id = field.getAnnotation(Id.class);
					String idName = id.name();
					if("".equals(idName)) {
						idName = field.getName();
					}
					sqlBuilder.append(idName);
					sqlBuilder.append(",");
					//values 后面拼接?
					valuesSqlBuilder.append("?,");
					//属性的值放入集合 
					params.add(getFieldValue(obj, field));
				} else if(field.isAnnotationPresent(Column.class)) {
					field.setAccessible(true);
					
					Column column = field.getAnnotation(Column.class);
					String columnName = column.name();
					if("".equals(columnName)) {
						columnName = field.getName();
					}
					sqlBuilder.append(columnName);
					sqlBuilder.append(",");
					//values 后面拼接?
					valuesSqlBuilder.append("?,");
					//属性的值放入集合 
					params.add(getFieldValue(obj, field));
				} else if(field.isAnnotationPresent(ManyToOne.class)) {
					field.setAccessible(true);
					
					ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
					String manyToOneName = manyToOne.name();
					//如果manyToOne的name为空串的时候，则使用对象的主键名称
					Map<String, Object> idNameValue = getIdNameValue(obj, field);
					if("".equals(manyToOneName)) {
						//获取到名称
						manyToOneName = (String) idNameValue.get("idName");
					}
					sqlBuilder.append(manyToOneName);
					sqlBuilder.append(",");
					//values 后面拼接?
					valuesSqlBuilder.append("?,");
					//属性的值放入集合 
					params.add(idNameValue.get("idValue"));
				} else if(field.isAnnotationPresent(OneToMany.class)) {
					Object fieldValue = getFieldValue(obj, field);
					oneToManyField.add((List) fieldValue);
				}
			}
			//循环完成
			String sql = sqlBuilder.substring(0, sqlBuilder.length()-1) + valuesSqlBuilder.substring(0, valuesSqlBuilder.length() - 1) + ")";
			//执行了当前对象的插入
			int result = update(sql, params.toArray());
			//对象里面的集合对象插入
			//循环每一个属性
			for (int i = 0; i < oneToManyField.size(); i++) {
				List list = oneToManyField.get(i);
				//循环属性中的每一个对象
				for (int j = 0; j < list.size(); j++) {
					insert(list.get(j));
				}
			}
			return result;
		}
		return -1;
	}
	
	
	/**
	 * 插入数据  返回数据库自动生成的id
	 * insert into 表名 (字段名,字段名...)  values (?,?,....)
	 * @param obj
	 * @return
	 */
	public static int insertForId(Object obj) {
		Class<? extends Object> clazz = obj.getClass();
		if(clazz.isAnnotationPresent(Table.class)) {
			StringBuilder sqlBuilder = new StringBuilder();//sql语句拼接
			StringBuilder valuesSqlBuilder = new StringBuilder();//sql语句拼接
			//存放onetomany的属性
			List<List> oneToManyField = new ArrayList<List>();
			
			List params = new ArrayList();//参数
			sqlBuilder.append("insert into ");
			valuesSqlBuilder.append(" ) values ( ");
			//获取注解对象
			Table table = clazz.getAnnotation(Table.class);
			String tableName = table.name();
			//如果注解的value为空串则表名为类名
			if("".equals(tableName)) {
				tableName = clazz.getSimpleName();
			}
			//拼接表名
			sqlBuilder.append(tableName);
			sqlBuilder.append(" ( ");
			//开始拼接属性
			Field[] fields = clazz.getDeclaredFields();
			//循环属性
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				//如果对象的属性为null则不进行操作
				if(getFieldValue(obj, field) == null) {
					continue;
				}
				//如果是主键
				if(field.isAnnotationPresent(Id.class)) {
					field.setAccessible(true);
					
					Id id = field.getAnnotation(Id.class);
					String idName = id.name();
					if("".equals(idName)) {
						idName = field.getName();
					}
					sqlBuilder.append(idName);
					sqlBuilder.append(",");
					//values 后面拼接?
					valuesSqlBuilder.append("?,");
					//属性的值放入集合 
					params.add(getFieldValue(obj, field));
				} else if(field.isAnnotationPresent(Column.class)) {
					field.setAccessible(true);
					
					Column column = field.getAnnotation(Column.class);
					String columnName = column.name();
					if("".equals(columnName)) {
						columnName = field.getName();
					}
					sqlBuilder.append(columnName);
					sqlBuilder.append(",");
					//values 后面拼接?
					valuesSqlBuilder.append("?,");
					//属性的值放入集合 
					params.add(getFieldValue(obj, field));
				} else if(field.isAnnotationPresent(ManyToOne.class)) {
					field.setAccessible(true);
					
					ManyToOne manyToOne = field.getAnnotation(ManyToOne.class);
					String manyToOneName = manyToOne.name();
					//如果manyToOne的name为空串的时候，则使用对象的主键名称
					Map<String, Object> idNameValue = getIdNameValue(obj, field);
					if("".equals(manyToOneName)) {
						//获取到名称
						manyToOneName = (String) idNameValue.get("idName");
					}
					sqlBuilder.append(manyToOneName);
					sqlBuilder.append(",");
					//values 后面拼接?
					valuesSqlBuilder.append("?,");
					//属性的值放入集合 
					params.add(idNameValue.get("idValue"));
				} else if(field.isAnnotationPresent(OneToMany.class)) {
					Object fieldValue = getFieldValue(obj, field);
					oneToManyField.add((List) fieldValue);
				}
			}
			//循环完成
			String sql = sqlBuilder.substring(0, sqlBuilder.length()-1) + valuesSqlBuilder.substring(0, valuesSqlBuilder.length() - 1) + ")";
			//执行了当前对象的插入
			int result = insert(sql, params.toArray());
			//对象里面的集合对象插入
			//循环每一个属性
			for (int i = 0; i < oneToManyField.size(); i++) {
				List list = oneToManyField.get(i);
				//循环属性中的每一个对象
				for (int j = 0; j < list.size(); j++) {
					insert(list.get(j));
				}
			}
			return result;
		}
		return -1;
	}
	
	
	/**
	 * 单表查询
	 * @param <T>
	 * @param clazz
	 * @param sql
	 * @param params
	 * @return
	 */
	public static <T> List<T> select(Class<T> clazz,String sql ,Object...params) {
		List<T> list = new ArrayList<T>();
		Connection conn = getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			pst = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pst.setObject(i+1, params[i]);
			}
			//得到结果集
			rs = pst.executeQuery();
			//处理结果集
			while(rs.next()) {
				//获取结果集的元数据对象
				ResultSetMetaData metaData = rs.getMetaData();
				//为每一个行的数据创建一个对象放入集合
				T obj = clazz.newInstance();
				//为对象进行赋值
				int columnCount = metaData.getColumnCount();
				for (int i = 1; i <= columnCount; i++) {
					//字段的名称
					String columnName = metaData.getColumnName(i);
					//获取到字段的值
					try {
						Field field = getField(clazz, columnName);
						setValue(obj, field, columnName, rs);
					} catch (RuntimeException e) {
					}
				}
				list.add(obj);
			}
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (InstantiationException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			close(rs, pst, conn);
		}
		return list;
	}
	
	/**
	 *     单表查询查询结果只能为一行的查询
	 * @param <T>
	 * @param clazz
	 * @param sql
	 * @param params
	 * @return
	 */
	public static <T> T selectOne(Class<T> clazz,String sql ,Object...params) {
		List<T> result = select(clazz, sql, params);
		return result.isEmpty() ? null : result.get(0);
	}
	/**
	 * 多表查询 查询对象的结果中有manyToOne的情况
	 * @param <T>
	 * @param clazz
	 * @param sql
	 * @param params
	 * @return
	 */
	public static <T> List<T> selectManyToOne(Class<T> clazz,String sql,Object...params) {
		List<T> list = new ArrayList<T>();
		Connection conn = getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			pst = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pst.setObject(i+1, params[i]);
			}
			//得到结果集
			rs = pst.executeQuery();
			//处理结果集
			while(rs.next()) {
				//获取结果集的元数据对象
				ResultSetMetaData metaData = rs.getMetaData();
				//为每一个行的数据创建一个对象放入集合
				T obj = clazz.newInstance();
				//为对象进行赋值
				int columnCount = metaData.getColumnCount();
				for (int i = 1; i <= columnCount; i++) {
					//字段的名称
					String columnName = metaData.getColumnName(i);
					//获取到字段的对象
					Field field = getManyToOneField(clazz, columnName);
					//赋值
					setValue(obj, field, columnName, rs);
				}
				list.add(obj);
			}
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (InstantiationException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			close(rs, pst, conn);
		}
		return list;
	}
	
	/**
	 * 通用查询
	 * @param sql
	 * @param params
	 * @return
	 */
	public static List<Map<String,Object>> select(String sql,Object...params){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Connection conn = getConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			pst = conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pst.setObject(i+1, params[i]);
			}
			//得到结果集
			rs = pst.executeQuery();
			//处理结果集
			while(rs.next()) {
				//获取结果集的元数据对象
				ResultSetMetaData metaData = rs.getMetaData();
				//为每一个行的数据创建一个Map放入集合
				Map<String,Object> map = new HashMap<String, Object>();
				//为对象进行赋值
				int columnCount = metaData.getColumnCount();
				for (int i = 1; i <= columnCount; i++) {
					//字段的名称
					String columnName = metaData.getColumnName(i);
					//获取到字段的对象
					Object value = rs.getObject(columnName);
					//把字段的名称和字段的值放入map
					map.put(columnName, value);
				}
				list.add(map);
			}
		} catch (SQLException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			close(rs, pst, conn);
		}
		return list;
	}
	
	/**
	 * 对对象的属性进行赋值
	 * @param obj
	 * @param field
	 * @param columnName
	 * @param rs
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	public static void setValue(Object obj,Field field,String columnName,ResultSet rs) throws SQLException, IllegalArgumentException, IllegalAccessException {
		field.setAccessible(true);
		Class<?> type = field.getType();
		if(type == Integer.class || type == int.class) {
			int value = rs.getInt(columnName);
			field.set(obj, value);
		} else if(type == Double.class || type == double.class) {
			double value = rs.getDouble(columnName);
			field.set(obj, value);
		} else if(type == String.class ) {
			String value = rs.getString(columnName);
			field.set(obj, value);
		} else if(type == Date.class ) {
			Date value = rs.getDate(columnName);
			field.set(obj, value);
		} 
		//如果要赋值的是对象
		if(field.isAnnotationPresent(ManyToOne.class)) {
			//如果对象是第一次赋值的话就创建一个新的对象，如果不是则直接使用之前的对象
			Object object = field.get(obj);
			field.setAccessible(true);
			if(object == null) {
				try {
					object = field.getType().newInstance();
				} catch (InstantiationException e) {
					e.printStackTrace();
				}
				field.set(obj, object);
			}
			//现在找到名称对应的字段 即可赋值
			Field f = null;
			try {
				f = getField(field.getType(), columnName);
			} catch (Exception e) {
				try {
					f = field.getType().getDeclaredField(getIdName(field.getType()));
				} catch (NoSuchFieldException e1) {
					e1.printStackTrace();
				} catch (SecurityException e1) {
					e1.printStackTrace();
				}
			}
			setValue(object, f, columnName, rs);
		}
	}
	
	
	
	
	/**
	 * 通过属性获取对应的值
	 * @param obj
	 * @param field
	 * @return
	 */
	public static Object getFieldValue(Object obj,Field field) {
		field.setAccessible(true);
		try {
			return field.get(obj);
		} catch (IllegalArgumentException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
//			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	/**
	 * 获取对应的属性对象
	 * @param clazz
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Field getField(Class<?> clazz,Object obj,String fieldName) {
		if(clazz != null) {
			try {
				return clazz.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
			} catch (SecurityException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		if(obj != null) {
			Class<? extends Object> class1 = obj.getClass();
			try {
				return class1.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
			} catch (SecurityException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return null;
	}
	/**
	 * 通过名称找到属性对象  先判断注解的name，在判断属性的属性名
	 * @param clazz
	 * @param columnName
	 * @return
	 */
	public static Field getField(Class<?> clazz,String columnName) {
		if(clazz != null) {
			try {
				Field f = null;//最后返回的属性
				Field[] fields = clazz.getDeclaredFields();
				for (Field field : fields) {
					if(field.isAnnotationPresent(Id.class)) {
						Id annotation = field.getAnnotation(Id.class);
						if(columnName.equals(annotation.name())) {
							f = field;
							break;
						}
					} else if(field.isAnnotationPresent(Column.class)) {
						Column annotation = field.getAnnotation(Column.class);
						if(columnName.equals(annotation.name())) {
							f = field;
							break;
						}
					}
				}
				//没有注解的name和他是相同，那只能是属性相同
				if(f == null) {
					f = clazz.getDeclaredField(columnName);
				}
				return f;
			} catch (NoSuchFieldException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
				
			} catch (SecurityException e) {
//				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return null;
	}
	/**
	 * 多表查询的时候去查找字段
	 * @param clazz
	 * @param columnName
	 * @return
	 */
	public static Field getManyToOneField(Class<?> clazz,String columnName) {
		Field field = null;
		try {
			//如果是普通字段 id和column
			field = getField(clazz, columnName);
		} catch (RuntimeException e) {
			//不是普通字段 而是manytoone的字段
			Field[] fields = clazz.getDeclaredFields();
			for (Field f : fields) {
				if(f.isAnnotationPresent(ManyToOne.class)) {
					ManyToOne annotation = f.getAnnotation(ManyToOne.class);
					//如果如果找的是主键
					if(columnName.equals(annotation.name())) {
						field = f;
						break;
					}
					if("".equals(annotation.name())) {
						String idName = getIdName(f.getType());
						if(columnName.equals(idName)) {
							field = f;
							break;
						}
					}
					//如果找的是类中的普通属性
					//如果找的是类中的普通属性
					try {
						getField(f.getType(), columnName);
						field = f;
					} catch (RuntimeException e2) {
						continue;
					}
					
				}
			}
			//如果这个类中不存在就返回主键
			if(field == null) {
					try {
						field = clazz.getDeclaredField(getIdName(clazz));
					} catch (NoSuchFieldException e1) {
						e1.printStackTrace();
					} catch (SecurityException e1) {
						e1.printStackTrace();
					}
			}
			
		}
		return field;
	}
	
	/**
	 * 找到一个类的主键的名称
	 * @param clazz
	 * @return
	 */
	public static String getIdName(Class<?> clazz) {
		String name = null;
		if(clazz.isAnnotationPresent(Table.class)) {
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				if(field.isAnnotationPresent(Id.class)) {
					Id annotation = field.getAnnotation(Id.class);
					name = annotation.name();
					if("".equals(name)) {
						name = field.getName();
					}
					break;
				}
			}
		}
		return name;
	}
	
	/**
	 * 获取属性的值
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Object getFieldValue(Object obj,String fieldName) {
		Field field = getField(null, obj, fieldName);
		if(field != null) {
			return getFieldValue(obj, field);
		}
		return null;
	}
	/**
	 * 从属性的对象上去获取对象的主键名称
	 * @param field
	 * @return
	 */
	public static Map<String, Object> getIdNameValue(Object obj,Field field) {
		field.setAccessible(true);
		//获取到属性的类型
		Class<?> type = field.getType();
		//判断属性的类型是否被Table标识
		if(type.isAnnotationPresent(Table.class)) {
			Field[] fields = type.getDeclaredFields();
			Map<String, Object> map = new HashMap<String, Object>();
			for (Field field2 : fields) {
				if(field2.isAnnotationPresent(Id.class)) {
					Id id = field2.getAnnotation(Id.class);
					String idName = id.name();
					if("".equals(idName)) {
						idName = field2.getName();
					}
					map.put("idName", idName);
					try {
						//从属性对象中去获取对象的主键值
						map.put("idValue", getFieldValue(field.get(obj), field2));
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
					break;
				}
			}
			return map;
		}
		throw new RuntimeException(field.getName() + "类型在数据库中没有匹配");
	}
	
	/*public static void main(String[] args) {
		String sql = "select * from groupmembers left join groups on groupmembers.gid=groups.gid left join user on groupmembers.uid=user.uid";
		List<GroupMembers> selectManyToOne = selectManyToOne(GroupMembers.class, sql);
		for (GroupMembers groupMembers : selectManyToOne) {
			System.out.println(groupMembers);
		}
//		String sql = "select * from groups";
//		List<Group> selectManyToOne = selectManyToOne(Group.class, sql);
//		for (Group group : selectManyToOne) {
//			System.out.println(group);
//		}
		
	}*/
}
